package com.wardrobe.soa.backend.operator;

import com.google.common.base.Splitter;
import com.wardrobe.mapper.master.admin_operator_manager.AdminOperatorMasterMapper;
import com.wardrobe.mapper.slave.admin_operator_manager.AdminOperatorSlaveMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("adminOperatorDao")
public class AdminOperatorDao {

    @Resource
    private AdminOperatorSlaveMapper adminOperatorSlaveMapper;

    @Resource
    private AdminOperatorMasterMapper adminOperatorMasterMapper;

    public AdminOperatorBean queryAdminById(int operatorId) {
        return adminOperatorSlaveMapper.queryAdminById(operatorId);
    }

    public List<Map<String, Object>> queryAdminList() {
        return adminOperatorSlaveMapper.queryAdminList();
    }

    public int addAdmin(AdminOperatorBean bean) {
        return adminOperatorMasterMapper.addOperator(bean);
    }

    public AdminOperatorBean findByUserName(String nickName) {
        return adminOperatorSlaveMapper.queryAdminByNickName(nickName);
    }

    public int delOperator(int operatorId) {
        return adminOperatorMasterMapper.delOperator(operatorId);
    }

    public int updateOperator(AdminOperatorBean bean) {
        return adminOperatorMasterMapper.updateOperator(bean);
    }

    public int resetPwd(AdminOperatorBean bean) {
        return adminOperatorMasterMapper.resetPwd(bean);
    }

    public int addRole(RoleBean bean) {
        return adminOperatorMasterMapper.addRole(bean);
    }

    public int addPermission(PermissionBean bean) {
        return adminOperatorMasterMapper.addPermission(bean);
    }

    public int delRole(RoleBean bean) {
        return adminOperatorMasterMapper.delRole(bean);
    }

    public int delPermission(PermissionBean bean) {
        return adminOperatorMasterMapper.delPermission(bean);
    }

    public List<RoleBean> queryAllRoles() {
        return adminOperatorSlaveMapper.queryRoleList();
    }

    public List<PermissionBean> queryAllPermissions() {
        return adminOperatorSlaveMapper.queryPermissionList();
    }

    public int updatePermission(PermissionBean bean) {
        return adminOperatorMasterMapper.updatePermission(bean);
    }

    public int updateRole(RoleBean bean) {
        return adminOperatorMasterMapper.updateRole(bean);
    }

    public List<Map<String, Object>> queryPermissionsByRoleId(int roleId) {
        return adminOperatorSlaveMapper.queryPermissionListByRoleId(roleId);

    }

    public List<Map<String, Object>> queryPermissionByOperatorId(int operatorId) {
        return adminOperatorSlaveMapper.queryPermissionListByOperatorId(operatorId);
    }

    public List<Map<String, Object>> queryRoleByOperatorId(int operatorId) {
        return adminOperatorSlaveMapper.queryRolesListByOperatorId(operatorId);
    }

    public int removePermissionFromRole(RoleBean bean) {
        return adminOperatorMasterMapper.removePermissionFromRole(bean);
    }

    public int addPermissionFromRole(RoleBean bean) {

        List<String> permissionList = Splitter.on(",").splitToList(bean.getPermission());
        return adminOperatorMasterMapper.addaddPermissionFromRoleBatch(bean.getId(), permissionList);
    }

    public int removeRoleFromOperator(AdminOperatorBean bean) {
        return adminOperatorMasterMapper.removeRoleFromOperator(bean);
    }

    public int addRoleFromOperator(AdminOperatorBean bean) {

        List<String> roleIds = Splitter.on(",").splitToList(bean.getRoleIds());
        return adminOperatorMasterMapper.addRoleFromOperatorBatch(bean.getId(), roleIds);
    }
}
